package com.dianrui.operation.activity;

import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dianrui.operation.R;
import com.dianrui.operation.okhttp.OkHttpRequest;
import com.dianrui.operation.util.SharedUtil;
import com.dianrui.operation.util.StringUtils;
import com.dianrui.operation.util.ToastUtil;
import com.dianrui.operation.util.Url;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class ForgetLoginPwd extends BaseActivity {
    boolean isHide = false;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.act_edit_login_pwd_et_tel)
    EditText etTel;
    @BindView(R.id.act_edit_login_pwd_et_psw_old)
    EditText etOldOne;
    @BindView(R.id.act_edit_login_pwd_et_psw_one)
    EditText etPswOne;
    @BindView(R.id.act_edit_login_pwd_et_psw_two)
    EditText etPswTwo;
    @BindView(R.id.current_pwd_eyes)
    ImageView currentPwdEyes;
    @BindView(R.id.new_pwd_eyes)
    ImageView newPwdEyes;
    @BindView(R.id.confirm_pwd_eyes)
    ImageView confirmPwdEyes;


    @Override
    public int getContentViewId() {
        return R.layout.activity_forget_pwd;
    }

    @Override
    public void initView() {
        title.setText("修改登录密码");
        if (!StringUtils.isEmpty(spUtils.getString("account"))) {
            etTel.setText(spUtils.getString("account"));
        }
        currentPwdEyes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isHide) {
                    isHide = false;
                    currentPwdEyes.setImageResource(R.mipmap.logo_eyes_open);
                    HideReturnsTransformationMethod method = HideReturnsTransformationMethod.getInstance();
                    etOldOne.setTransformationMethod(method);
                    etOldOne.setSelection(etOldOne.length());
                } else {
                    isHide = true;
                    currentPwdEyes.setImageResource(R.mipmap.logo_eyes_close);
                    TransformationMethod method = PasswordTransformationMethod.getInstance();
                    etOldOne.setTransformationMethod(method);
                    etOldOne.setSelection(etOldOne.length());
                }
            }
        });
        newPwdEyes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isHide) {
                    isHide = false;
                    newPwdEyes.setImageResource(R.mipmap.logo_eyes_open);
                    HideReturnsTransformationMethod method = HideReturnsTransformationMethod.getInstance();
                    etPswOne.setTransformationMethod(method);
                    etPswOne.setSelection(etPswOne.length());
                } else {
                    isHide = true;
                    newPwdEyes.setImageResource(R.mipmap.logo_eyes_close);
                    TransformationMethod method = PasswordTransformationMethod.getInstance();
                    etPswOne.setTransformationMethod(method);
                    etPswOne.setSelection(etPswOne.length());
                }
            }
        });

        confirmPwdEyes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isHide) {
                    isHide = false;
                    confirmPwdEyes.setImageResource(R.mipmap.logo_eyes_open);
                    HideReturnsTransformationMethod method = HideReturnsTransformationMethod.getInstance();
                    etPswTwo.setTransformationMethod(method);
                    etPswTwo.setSelection(etPswTwo.length());
                } else {
                    isHide = true;
                    confirmPwdEyes.setImageResource(R.mipmap.logo_eyes_close);
                    TransformationMethod method = PasswordTransformationMethod.getInstance();
                    etPswTwo.setTransformationMethod(method);
                    etPswTwo.setSelection(etPswTwo.length());
                }
            }
        });
    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.back, R.id.update_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.update_pwd:
                String oldpwd = etOldOne.getText().toString();//旧密码
                String psw1 = etPswOne.getText().toString().trim();//新密码
                String psw2 = etPswTwo.getText().toString().trim();//确认新密码

                if (StringUtils.isEmpty(oldpwd)) {
                    ToastUtil.showToast("请输入旧密码");
                    return;
                }
                if (StringUtils.isEmpty(psw1)) {
                    ToastUtil.showToast("请输入新密码");
                    return;
                }
                if (StringUtils.isEmpty(psw2)) {
                    ToastUtil.showToast("请输入确认密码");
                    return;
                }

                if (!psw1.equals(psw2)) {
                    ToastUtil.showToast("两次密码不一致,请重新确认");
                    return;
                }
                modifyLoginPwd();
                break;

            default:
                break;
        }
    }

    public void modifyLoginPwd() {
        JsonObject forgetpwd = new JsonObject();
        forgetpwd.addProperty("old_password", etOldOne.getText().toString());
        forgetpwd.addProperty("new_password", etPswOne.getText().toString());
        OkHttpRequest.getInstance().postRequests(SharedUtil.getUrl() + Url.SETINFO, forgetpwd.toString(), new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if ("1000".equals(jsonObject.optString("status"))) {
                            spUtils.clear();
                            ToastUtil.showToast(jsonObject.optString("message"));
                            JumpActivitys(LoginActivity.class);
                            finish();
                        } else if ("1001".equals(jsonObject.optString("status"))) {
                            ToastUtil.showToast(jsonObject.optString("message"));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));

            }
        });
    }
}
